
%================================
%xu ly anh demo ve binary de tao test
%input cua ham se la anh I3 thuc hien o cac buoc truoc

function [listLP, listCharacter, listPostion] = main (Io, IS_DEBUG, ...
     file_name, FOLDER_RESULT, ...
    conSauvola, conFilter1, conFilter2)
tic;

[pathstr, name, ext] = fileparts(file_name);
ext = '.JPG';
if (IS_DEBUG)
    imwrite(Io,strcat(FOLDER_RESULT,'\', ...
        name,'_GrayImage',ext),'JPG');
end
IograyRecive = Io;

% Io = imresize(Io,[1600 NaN]);

%% Thuc hien Sauvola
% conSauvola(1) = 0.1;
% conSauvola(3) = 5;
Isau = Sauvola(Io, conSauvola);


if (IS_DEBUG)
imwrite(Isau,strcat(FOLDER_RESULT,'\',name,'_BinarySauvola',ext),'JPG');
end

% %% Thuc hien edge detection
Iedgedetect = EdgeDetect(Io);
% % IS_DEBUG = false;
if (IS_DEBUG)
    imwrite(Iedgedetect,strcat(FOLDER_RESULT,'\', ...
        name,'_BinaryEdgeDetect',ext),'JPG');
end
%
%% Thuc hien hough transform demo
%Do the Hough transform
% [H t r] = hough(Isau); %r and c store column labels...
%
% %Display the transform in such a way that we can draw points on it later...
% imshow(H, [], 'XData', t, 'YData', r );
%
% %Add axis labels to the picture
% xlabel('\theta'), ylabel('\rho');
% axis on, axis normal;




% %% And image with edge detect and sauvola
I3 = EdgeAndSauvola(Iedgedetect, Isau);
%
%DEBUG
% IS_DEBUG = false;
if (IS_DEBUG)
    imwrite(I3,strcat(FOLDER_RESULT,'\', ...
        name,'_EdgeAndSauvola',ext),'JPG');
end

%% Thuc hien measureFilter
if (IS_DEBUG)
    if exist(strcat(FOLDER_RESULT,'\',name),'dir') == 0
        mkdir(strcat(FOLDER_RESULT,'\',name));
    end
end

result={};
resultF={};
arrayResultPosition = {};
[ arrayResultBinary, arrayResultOrg, arrayResultPosition, color_binary, IedgeEdit] = MeasureFilter(I3,  Iedgedetect, Io, ...
    IS_DEBUG, conFilter1,FOLDER_RESULT, name ,result, resultF, arrayResultPosition );  % mang chua cac anh thoa dieu kien duoc chon ra
if IS_DEBUG
imwrite(color_binary,strcat(FOLDER_RESULT,'\',name,'_color',ext),'JPG');
end

%% Tien hanh loc lai lan thu 2 cho nung truong hop bi nhieu o vien xe
IedgeEdit = imdilate(IedgeEdit,strel('square',2));
% IedgeEdit = imdilate(IedgeEdit,strel('disk',2));
I3Edit = EdgeAndSauvola(IedgeEdit, Isau);
[ arrayResultBinary, arrayResultOrg, arrayResultPosition, color_binary, IedgeEdit] = MeasureFilter(I3Edit,  IedgeEdit, Io, ...
    IS_DEBUG, conFilter1,FOLDER_RESULT, name ,arrayResultBinary, arrayResultOrg, arrayResultPosition ); 
if IS_DEBUG
imwrite(color_binary,strcat(FOLDER_RESULT,'\',name,'_colorEdit',ext),'JPG');
end

% 
% %% Tien hanh loc lai lan tiep theo nua
% IedgeEdit = imdilate(IedgeEdit,strel('disk',1));
% I3Edit = EdgeAndSauvola(IedgeEdit, Isau);
% [ arrayResultBinary, arrayResultOrg, color_binary, IedgeEdit] = MeasureFilter(I3Edit,  IedgeEdit, indexFigure, Io, ENABLE_GAUSS, ...
%     IS_DEBUG, conFilter1,FOLDER_RESULT, name ,arrayResultBinary, arrayResultOrg ); 
% if IS_DEBUG
% imwrite(color_binary,strcat(FOLDER_RESULT,'\',name,'_colorEdit2',ext),'JPG');
% end

%% tien hanh hien thi cac anh phan loai duoc

[arrImageRe ,arrayImageResultColor, arrayImagResPosition,  a] = DisplayImage(arrayResultBinary, arrayResultOrg, arrayResultPosition, IS_DEBUG,  conFilter2, name, FOLDER_RESULT);
listLP = arrImageRe;
listCharacter = {};
listPostion = arrayImagResPosition;

%% nhan dang cac ky tu trong bien so
% [finalArrPlate, finalArrPlateColor,  finalArrCharacter, finalArrayPosition] = characterSegment.StepRecognizeCharacter(arrImageRe, arrayImageResultColor, arrayImagResPosition, FOLDER_RESULT, IS_DEBUG,file_name);

 
% [finalArrPlate, finalArrPlateColor,  finalArrCharacter] = StepRecognizeCharacter(arrImageRe, arrayImageResultColor,FOLDER_RESULT, IS_DEBUG,file_name);
% listLP = finalArrPlate;
% listCharacter = finalArrCharacter;
% lsPlateColor = finalArrPlateColor;

%% draw rectangle reion detect to orignal image
% for j=1:length(finalArrayPosition)
%     shapeInserter = vision.ShapeInserter('Shape','Rectangles','BorderColor','Custom', 'CustomBorderColor', uint8([255 0 0]));
%     rectangle = int32(finalArrayPosition{j});    
%     IshowResultFinal = step(shapeInserter, IshowResultFinal, rectangle);
%     rectangle(1) = rectangle(1)-1;
%     rectangle(2) = rectangle(2)-1;
%     rectangle(3) = rectangle(3)+2;
%     rectangle(4) = rectangle(4)+2;
%     IshowResultFinal = step(shapeInserter, IshowResultFinal, rectangle);
% end
% IshowResultFinalOut = IshowResultFinal;

toc;
end




